# vector_functions.py
#
# Functions to manipulate mathematical vectors represented as lists

import math
from math_functions import *

import unittest

def zeroVector(dim):
    return map(lambda index: 0.0, range(dim))

def crossProduct3d(u, v):
    cp_x = u[1]*v[2] - v[1]*u[2]
    cp_y = u[2]*v[0] - v[2]*u[0]
    cp_z = u[0]*v[1] - v[0]*u[1]
    return [cp_x, cp_y, cp_z]

def dotProduct(u, v):
    return reduce(add, map(multiply, u, v))

def magnitude(v):
    return math.sqrt(dotProduct(v, v))

if __name__ == '__main__':
    class TestVectorFunctions(unittest.TestCase):
    
        def test_zero_vector(self):
            self.assert_(zeroVector(3) == [0.0, 0.0, 0.0])

        def test_dot_product(self):
            self.assert_(dotProduct([1.0, 1.0], [2.0, 3.0]) == 5.0)
            self.assert_(dotProduct([1.0, 2.0, 3.0], [0.5, 1.25, -10.5612]) ==
                         1.0*0.5+2.0*1.25+3.0*-10.5612)

        def test_magnitude(self):
            self.assert_(magnitude([3.0, 4.0]) == 5.0)
            self.assert_(magnitude([1.0, 1.0, 1.0]) == math.sqrt(3.0))

    unittest.main()
